﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExcelTool {
    public class StringBuilderSelector {
        public JSON_MODE mode = JSON_MODE.COMMON;
        public StringBuilder client_sb = new StringBuilder();
        public StringBuilder server_sb = new StringBuilder();

        public bool no_comma = false;
        public bool no_comma_server = false; 
    }
    static class FormatHelper {

        #region common
        public static void AppendIndent(StringBuilder m_sb, int level = 1) {
            for(int i = 0; i < level; i++) {
                m_sb.Append(FILE_FORMAT.INDENT); 
            }
        }

        /// <summary>
        /// 通用的内容Append函数
        /// </summary>
        /// <param name="result">输出对象</param>
        /// <param name="content">输出内容</param>
        /// <param name="indent_level">缩进等级</param>
        /// <param name="data_content">是否为数据（数据根据外部参数决定是否要缩进，非数据格式一律都需要缩进）</param>
        static void _appendContent(StringBuilder result, string content, int indent_level, bool data_content) {
            if(data_content && Converter.DataCompact) {
                result.Append(content);
            }
            else {
                AppendIndent(result, indent_level);
                result.AppendLine(content);
            }
        }

        static string _makeContentWithComma(string content, bool no_comma) {
            if(!no_comma) {
                return string.Concat(content, ',');
            }
            return content;
        }
        static void _appendContent(StringBuilderSelector result, string content, int indent_level, bool data_content) {
            JSON_MODE mode = result.mode;
            if(mode == JSON_MODE.COMMON || mode == JSON_MODE.CLIENT) {
                _appendContent(result.client_sb, _makeContentWithComma(content, result.no_comma), indent_level, data_content);
            }
            if(mode == JSON_MODE.COMMON || mode == JSON_MODE.SERVER) {
                _appendContent(result.server_sb, _makeContentWithComma(content, result.no_comma_server), indent_level, data_content);
            }
        }
        public static void AppendComma(StringBuilder result) {
            result.Append(",");
        }

        #endregion


        #region json format
        public static void JsonLeftBrace(StringBuilderSelector sb, ref int indent) {
            sb.no_comma = true;
            sb.no_comma_server = true;
            _appendContent(sb, "{", indent++, true);
        }
        public static void JsonRightBrace(StringBuilderSelector sb, ref int indent, bool no_comma = false, bool no_comma_server = false) {
            sb.no_comma = no_comma;
            sb.no_comma_server = no_comma_server;
            _appendContent(sb, "}", --indent, true);
        }
        public static void JsonLeftBracket(StringBuilderSelector sb, ref int indent) {
            sb.no_comma = true;
            sb.no_comma_server = true;
            _appendContent(sb, "[", indent++, true);
        }
        public static void JsonRightBracket(StringBuilderSelector sb, ref int indent, bool no_comma = false, bool no_comma_server = false) {
            sb.no_comma = no_comma;
            sb.no_comma_server = no_comma_server;
            _appendContent(sb, "]", --indent, true);
        }
        public static void JsonContent(StringBuilderSelector sb, string content, int indent, bool no_comma = false, bool no_comma_server = false){
            sb.no_comma = no_comma;
            sb.no_comma_server = no_comma_server;
            _appendContent(sb, content, indent, true);
        }
        #endregion


        #region cs format
        public static void CSharpHeader(StringBuilder m_sb) {
            m_sb.AppendLine("using System;");
            m_sb.AppendLine("using System.Collections.Generic;");
        }
        public static void CodeLeftBracket(StringBuilder sb, ref int indent) {
            _appendContent(sb, "{", indent++, false);
        }
        public static void CodeRightBracket(StringBuilder sb, ref int indent) {
            _appendContent(sb, "}", --indent, false);
        }

        public static void CodeContent(StringBuilder sb, string content, int indent) {
            _appendContent(sb, content, indent, false);
        }

        public static string MakeCSMetaName(XmlMetalib lib) {
            return string.Format("MT_{0}", lib.lib_name); 
        }

        public static string MakeCSSetName(XmlBaseSet base_set) {
            return string.Format("MT_{0}_{1}", base_set.ParentLib.lib_name, base_set.name);
        }
        public static string MakeGroupWrapperClass(XmlGroupSet group) {
            return string.Format("MT_GROUP_{0}_{1}", group.ParentLib.lib_name, group.name);
        }
        public static string MakeCSEnumName(XmlBaseSet baseSet) {
            return string.Format("E_MT_{0}_{1}", baseSet.ParentLib.lib_name.ToUpper(), baseSet.name.ToUpper());
        }
        #endregion

    }
}
